﻿
////////////////////////////////////////////////////////////////////////////////////////////
// 文件名: SSConfigDAL.gen.cs
//
// 模块名称: SSConfigDal
//
// 作者: LPC
//
// 历史记录（记录修改记录，修改文件后请添加修改记录，注明修改时间、修改人，修改内容）:
// 13-1-22    LPC    创建文件
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using Common;
using Model;

namespace DAL
{
    /// <summary>
    /// SSConfig表数据访问层
    /// </summary>
    public class SSConfigDAL: DB
    {
        public SSConfigDAL(LoginInfo info)
        {
            base.loginInfo = info;
        }
        /// <summary>
        /// 根据GUID字段获取SSConfigInfo实体对象
        /// </summary>
        /// <param name="guid">GUID</param>
        /// <returns>SSConfigInfo实体对象</returns>
        public SSConfigInfo GetInfoById(string guid)
        {
            var list = new List<SSConfigInfo>();

            //创建DbCommand对象
            HIDB.dbo cmd = base.conn();

            //添加输入输出参数
            IDataParameter[] sqlparams = { 
             new SqlParameter("@GUID",guid)    
            };

            //执行命令返回结果
            try
            {
                DataTable dt = cmd.StoredDataTable("Working_db.UP_SSConfig_Select", sqlparams);
                list = CreateSSConfigInfo(dt);
            }
            catch (Exception ex)
            {
                Logger.LogError("Working_db.UP_SSConfig_Select", "Working_db.UP_SSConfig_Select", 0, 1, ex, "", "");
            }
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        /// <summary>
        /// 根据GUID字段获取SSConfigInfo实体对象
        /// </summary>
        /// <param name="par"> </param>
        /// <returns>SSConfigInfo实体对象</returns>
        public List<SSConfigInfo> GetInfoList(IDataParameter[] par)
        {
            var list = new List<SSConfigInfo>();

            //创建DbCommand对象
            HIDB.dbo cmd = base.conn();
            //执行命令返回结果
            try
            {
                DataTable dt = cmd.StoredDataTable("Working_db.UP_SSConfig_SelectList", par);
                list = CreateSSConfigInfo(dt);
            }
            catch (Exception ex)
            {
                Logger.LogError("Working_db.UP_SSConfig_SelectList", "Working_db.UP_SSConfig_SelectList", 0, 1, ex, "", "");
            }
            if (list != null)
            {
                return list;
            }
            return null;
        }

        /// <summary>
        /// 往SSConfig表内添加新记录
        /// </summary>
        /// <returns>添加记录索引值</returns>
        public int Insert(IDataParameter[] para)
        {
            int num = -1;
            //创建cmd对象
            HIDB.dbo cmd = base.conn();
            //执行命令返回结果
            try
            {
                cmd.StoredUpdate("Working_db.UP_SSConfig_Inser", para);
                num = 1;
            }
            catch (Exception ex)
            {
                Logger.LogError("Working_db.UP_SSConfig_Inser", "Working_db.UP_SSConfig_Inser", 0, 1, ex, "", "");
            }
            return num;
        }

        /// <summary>
        /// 更新SSConfig表记录
        /// </summary>
        /// <param name="sSConfigInfo"> </param>
        public int Update(SSConfigInfo sSConfigInfo)
        {
            int num = -1;

            //创建cmd对象
            HIDB.dbo cmd = base.conn();

            //添加输入输出参数
            IDataParameter[] sqlparams = {
            new SqlParameter("@Guid",sSConfigInfo.Guid),
            new SqlParameter("@Name",sSConfigInfo.Name),
            new SqlParameter("@DatabaseType",sSConfigInfo.DatabaseType),
            new SqlParameter("@ConnectionString",sSConfigInfo.ConnectionString)
                                         };

            //执行命令返回结果
            try
            {
                cmd.StoredUpdate("Working_db.UP_SSConfig_Update", sqlparams);
                num = 1;
            }
            catch (Exception ex)
            {
                Logger.LogError("Working_db.UP_SSConfig_Update", "Working_db.UP_SSConfig_Update", 0, 1, ex, "", "");
            }

            return num;
        }

        /// <summary>
        /// 根据GUID字段删除SSConfigInfo表信息
        /// </summary>
        /// <param name="guid">GUID</param>
        public int Delete(string guid)
        {
            int num = -1;

            //创建Database对象          
            HIDB.dbo cmd = base.conn();

            //添加输入输出参数
            IDataParameter[] sqlparams = {
            new SqlParameter("@GUID",guid)
                                         };

            //执行命令返回结果
            try
            {
                cmd.StoredUpdate("UP_Sys_RoleInfo_Insert", sqlparams);
                num = 1;
            }
            catch (Exception ex)
            {
                Logger.LogError("UP_Sys_RoleInfo_Insert", "UP_Sys_RoleInfo_Insert", 0, 1, ex, "", "");
            }
            return num;
        }

        /// <summary>
        /// 生成SSConfigInfo实体对象
        /// </summary>
        private List<SSConfigInfo> CreateSSConfigInfo(DataTable dt)
        {
            var list = new List<SSConfigInfo>();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                var mySSConfigInfo = new SSConfigInfo
                {
                    Guid = DBConvert.GetString(dt.Rows[j], "GUID"),
                    Name = DBConvert.GetString(dt.Rows[j], "Name"),
                    DatabaseType = DBConvert.GetInt32(dt.Rows[j], "DatabaseType"),
                    ConnectionString = DBConvert.GetAnsiString(dt.Rows[j], "ConnectionString")
                };
                list.Add(mySSConfigInfo);
            }
            return list;
        }
    }
}